package midware

import (
	"github.com/gin-gonic/gin"
)

func Cors(c *gin.Context) {
	c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS") // 不要写成*，DELETE和PUT必须明确写出来
	c.Header("Access-Control-Allow-Headers", "*")
	origin := c.GetHeader("Origin")
	if origin == "" {
		origin = "*"
	}
	c.Header("Access-Control-Allow-Origin", origin)
	// c.Header("Access-Control-Allow-Credentials", "false") // don't allow cookie

	if c.Request.Method == "OPTIONS" {
		c.String(200, "")
		c.Abort()
	}
}
